package com.xuanmiao.smsback.web.controller.operator;

import com.xuanmiao.smsback.domain.qo.SensitiveWordsInfoQO;
import com.panshi.hujin2.base.common.factory.MessageFactory;
import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.panshi.hujin2.base.domain.result.BasicResultCode;
import com.xuanmiao.smsback.web.controller.vo.SensitiveWordsInfoVO;

import com.xuanmiao.smsback.model.SensitiveWordsInfoBO;
import com.xuanmiao.smsback.service.platform.operator.SensitiveWordsInfoService;

import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@Api("地区营运配置")
@RequestMapping(value = "/senstiveWordsInfo")
@RestController
public class SensitiveWordsInfoController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private SensitiveWordsInfoService sensitiveWordsInfoService;

    @ApiOperation("地区敏感词配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status" ,value = "状态 0--停用 1--激活" ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "countryCallingCode" ,value = "地区码" ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "nameInChinese" ,value = "国家中文名" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "nameInEnglish" ,value = "国家英文名" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "sensitiveWord" ,value = "敏感词" ,dataType = "String",paramType = "query"),

    })
    @GetMapping()
    BasicResult<List<SensitiveWordsInfoVO>> all(@ApiIgnore SensitiveWordsInfoQO sensitiveWordsInfoQO){
        try {
            List<SensitiveWordsInfoBO> listBOs = sensitiveWordsInfoService.getList(sensitiveWordsInfoQO);
            List<SensitiveWordsInfoVO> listVOs = Collections.emptyList();
            listVOs = DozerUtils.convertList(listBOs, SensitiveWordsInfoVO.class);
            return BasicResult.ok(listVOs, sensitiveWordsInfoQO.getPage());
        }catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询地区敏感词数据")
    @GetMapping("{id:\\d+}")
    BasicResult<SensitiveWordsInfoVO> queryPrimaryKey(@PathVariable Integer id) {
        try {
            SensitiveWordsInfoBO res = sensitiveWordsInfoService.getById(id);
            return BasicResult.ok(DozerUtils.convert(res, SensitiveWordsInfoVO.class));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("地区敏感词更新编辑")
    @PutMapping("/update")
    BasicResult<Void> update(@Valid @RequestBody SensitiveWordsInfoVO sensitiveWordsInfoVO,
                             @ApiParam("用户id") @RequestHeader("sms-uid") Integer userId) {

        try {
            SensitiveWordsInfoBO res = sensitiveWordsInfoService.getById(sensitiveWordsInfoVO.getId());
            if (res == null) {
                return BasicResult.error(BasicResultCode.DATA_NOT_EXIST.getCode(), BasicResultCode.DATA_NOT_EXIST.getMessage() );
            }

            sensitiveWordsInfoVO.setUpdatorId(userId);
            sensitiveWordsInfoService.update(DozerUtils.convert(sensitiveWordsInfoVO, SensitiveWordsInfoBO.class));
            return BasicResult.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.UPDATE_FAIL.getCode(),BasicResultCode.UPDATE_FAIL.getMessage() );
        }
    }
}
